package com.orangelife.mobile.myhouse.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.IntentHelper;
import com.curry.android.util.PhoneCallUtil;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.datatrack.DataTrack;
import com.orangelife.mobile.app.application.CurryApplication;
import com.common.jump.HouseJumper;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.myhouse.biz.HouseBiz;
import com.orangelife.mobile.myhouse.biz.HouseHttpBiz;
import com.orangelife.mobile.util.ToastHelper;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 通用消息提示类
 * <p/>
 * Created by 吴格非 on 2015/11/12.
 */
public class HouseTipActivity extends OrangeLifeBaseActivity {
    @Bind(R.id.ivIcon)
    ImageView ivIcon;
    @Bind(R.id.tvTip)
    TextView tvTip;
    @Bind(R.id.btnHouse)
    Button btnHouse;
    /** 尚未处理的消息顶部条 */
    @Bind(R.id.includeWaitConfirmBar)
    View includeWaitConfirmBar;
    /** 跳转到待处理界面 */
    @Bind(R.id.tvJumpWaitConfirm)
    TextView tvJumpWaitConfirm;
    /** 关闭顶部条 */
    @Bind(R.id.ivClose)
    ImageView ivClose;
    /** 标题栏 */
    @Bind(R.id.ib_title_left)
    LinearLayout ibTitleLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_title_right)
    ImageView ivTitleRight;

    private String tip;
    private String applicationID;
    private String phone;

    public static final String INTENT_KEY_HOUSE_TIP = "HOUSE_TIP";
    /** 未认证 */
    public static final String TYPE_TIP_UNCERT = "0";
    /** 申请信息审核失败 */
    public static final String TYPE_TIP_INFO_ERROR = "1";
    /** 验证码过期 */
    public static final String TYPE_TIP_CODE_EXPAIR = "2";
    /** 邀请成功 */
    public static final String TYPE_TIP_INVITE_OK = "3";

    private String currentType = TYPE_TIP_UNCERT;

    /**Activit 名称**/
    final  String MODULE_NAME="/IA/IAFail";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_tip);
        ButterKnife.bind(this);
        getIntentInfo();
        showByType();
        initTitle();
        CurryApplication.getInstance().addActivity(this);
    }

    private void getIntentInfo() {
        currentType = getIntent().getStringExtra(INTENT_KEY_HOUSE_TIP);
        tip = getIntent().getStringExtra("house_tip_value");
        phone = getIntent().getStringExtra("house_tip_phone");
        applicationID = getIntent().getStringExtra("applicationID");
    }

    protected void onResume() {
        super.onResume();
        if (TYPE_TIP_UNCERT.equals(currentType)) {
            requestWaitConfirmCount();
        }
    }

    /**
     * 数据采集体不要删除
     * @return
     */
    public Map<String,Object> getDataTrack(){
        Map<String,Object> map=new HashMap<>();
        map.put("UI_HOST", DataTrack.APP_NAME);
        map.put("UI_PATH", MODULE_NAME);
        map.put("IAFailResult", tip);
        return map;
    }

    /** 请求待认证条数 */
    private void requestWaitConfirmCount() {
        HouseHttpBiz.requestWaitConfirmCount(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                HashMap<String, String> map = HouseHttpBiz.handleWaitConfirmCount(response);
                HouseBiz.handleConfirmWaitBar(includeWaitConfirmBar, tvJumpWaitConfirm, map);
            }
        });
    }

    @OnClick({R.id.tvJumpWaitConfirm, R.id.ivClose})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvJumpWaitConfirm:
                HouseJumper.switchJump(HouseTipActivity.this, v);
                CurryApplication.getInstance().finishActivity(HouseTipActivity.this);
                break;
            case R.id.ivClose:
                includeWaitConfirmBar.setVisibility(View.GONE);
                break;
        }
    }

    @OnClick({R.id.ib_title_left, R.id.iv_title_right})
    public void onClickTitle(View view) {
        switch (view.getId()) {
            case R.id.ib_title_left:
                CurryApplication.getInstance().finishActivity(this);
                break;
            case R.id.iv_title_right:
                if (phone == null) {
                    ToastHelper.getInstance()._toast("对不起，获取电话失败");
                    return;
                } else {
                    PhoneCallUtil.getInstances().callPhone(HouseTipActivity.this, phone, "是否拨打物业电话(" + phone + ")？");
                }
                DataTrack.clickTrack(DataTrack.APP_NAME,MODULE_NAME,"call");
                break;
        }
    }

    private void initTitle() {
        tvTitle.setText("身份认证");
        if (currentType != null && currentType.equals(TYPE_TIP_INFO_ERROR)) {
            ivTitleRight.setVisibility(View.VISIBLE);
            ivTitleRight.setBackgroundResource(R.drawable.ic_house_phone);
        }
    }

    public void onBackClick(View view) {
        CurryApplication.getInstance().finishActivity(this);
    }

    private void showByType() {
        int resID = R.drawable.ic_house_tip_warn;
        String strBtnText = "申请认证";
        Map<String,String> map=new HashMap<>();
        switch (currentType) {
            case TYPE_TIP_UNCERT:
                resID = R.drawable.ic_house_tip_warn;
                if (StringUtil.isBlank(tip)) {
                    tip = "您还没有认证噢！";
                }
                break;
            case TYPE_TIP_INVITE_OK:
                resID = R.drawable.ic_house_tip_ok;
                if (StringUtil.isBlank(tip)) {
                    tip = "邀请发送成功！请耐心等待。";
                }
                strBtnText = "继续邀请";
                break;
            case TYPE_TIP_INFO_ERROR:
                resID = R.drawable.ic_house_tip_warn;
                if (StringUtil.isBlank(tip)) {
                    tip = "您提交的户主信息与物业信息不匹配, 请核对后重新申请, 或者您可以直接点击右上角的[一键拨号]与物业人员联系, 谢谢";
                }
                strBtnText = "重新申请";
                map.put("error",tip);

                break;
            case TYPE_TIP_CODE_EXPAIR:
                resID = R.drawable.ic_house_tip_warn;
                if (StringUtil.isBlank(tip)) {
                    tip = "您的验证码已过期";
                }
                strBtnText = "重新申请";
                map.put("error",tip);

                break;
            default:
                strBtnText = "确认";
                ToastHelper.getInstance()._toast("未知类型");
                map.put("error",tip);

                break;
        }
        ivIcon.setImageResource(resID);
        tvTip.setText(tip);
        btnHouse.setText(strBtnText);
    }

    @OnClick(R.id.btnHouse)
    @SuppressWarnings("unused")
    public void clickBtn(View v) {
        switch (currentType) {
            case TYPE_TIP_UNCERT:
                DataTrack.clickTrack(DataTrack.APP_NAME,MODULE_NAME,"reapply");
                IntentHelper.getInstances().getIntent(HouseTipActivity.this, HouseCertActivity.class);
                CurryApplication.getInstance().finishActivity(this);
                break;
            case TYPE_TIP_INVITE_OK:
                CurryApplication.getInstance().finishActivity(this);
                break;
            case TYPE_TIP_INFO_ERROR:
                DataTrack.clickTrack(DataTrack.APP_NAME,MODULE_NAME,"reapply");
                CurryApplication.getInstance().finishActivity(this);
                break;
            case TYPE_TIP_CODE_EXPAIR:
                if (applicationID != null) {
                    HouseJumper.jumpToHouseCode(HouseTipActivity.this, applicationID);
                    CurryApplication.getInstance().finishActivity(this);
                } else {
                    ToastHelper.getInstance()._toast("对不起，获取认证ID失败");
                }
                break;
            default:
                ToastHelper.getInstance()._toast("未知类型");
                CurryApplication.getInstance().finishActivity(this);
                break;
        }
    }
}
